package app;

import clustering.DBSCAN;
import googleSearch.GoogleQuery;
import java.awt.Dimension;
import java.io.IOException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import org.apache.lucene.index.TermFreqVector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import clustering.ClusteringAlgorithm;
import clustering.DBSCANv2;
import clustering.Kmeans2;
import clustering.Kmeans3;
import clustering.OPTICS;
import java.util.Enumeration;
import twitterSearch.SearchTwitter;
import twitterSearch.TwitterLuceneIndex;
import twitterSearch.TwitterLuceneSearch;
import twitter4j.QueryResult;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;

public class App extends javax.swing.JFrame {
    
    public static final int MAX_NUM_TERMS_FOR_EXPANSION = 6;
    private final int TOP_QUANTOS = 2;
    private Twitter twitter;
    private SearchTwitter twitterSearchEngine;
    private final String lang = "en";
    private Directory directory;
    private TwitterLuceneIndex luceneIndex;
    private TwitterLuceneSearch luceneSearch;
    private TermFreqVector[] vectors;
    private int numVectors;
    private ClusteringAlgorithm clusteringAlgo;
    private LinkedList<String> topTerms;
    private GoogleQuery googleSearchEngine;    
    
    public App() throws IOException, TwitterException {
        initComponents();
        this.googleSearchEngine = new GoogleQuery();
        this.twitter = getTwitter();
        this.twitterSearchEngine = new SearchTwitter(twitter);
        this.directory = getFDSDirectory();
        this.luceneIndex = new TwitterLuceneIndex(directory);
    }

    private Directory getFDSDirectory() throws IOException{
        String indexDir = "C:\\index-dir";
        return FSDirectory.getDirectory(indexDir, true);
    }
    
    protected void showResultsList(LinkedList<Tweet> tweetList){
          JFrame resultDialog = new JFrame();
          String[] tweetArr = new String[tweetList.size()];

          for (int i = 0; i < tweetList.size(); i++)
                tweetArr[i] = i + ") " + tweetList.get(i).getFromUser() + ":  "
                        + tweetList.get(i).getText();
          
          JList list = new JList(tweetArr);
          JScrollPane scrollPane = new JScrollPane(list,
                  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, // vertical bar
                  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          resultDialog.add(scrollPane);

          resultDialog.setSize(new Dimension(1100, 320));
          resultDialog.setPreferredSize(new Dimension(1100, 320));
          resultDialog.setLocationRelativeTo(null);
          resultDialog.setVisible(true);
      }
    
    protected JList getResultsJList(LinkedList<Tweet> tweetList){
          String[] tweetArr = new String[tweetList.size()];

          for (int i = 0; i < tweetList.size(); i++)
                tweetArr[i] = i + ") " + tweetList.get(i).getFromUser() + ":  "
                        + tweetList.get(i).getText();
          
          JList list = new JList(tweetArr);
          return list;
      }
        
    protected String getResultsString (LinkedList<Tweet> tweetList){
          String results = new String();
          results = results.concat("TWITTER RESULTS:");

          for (int i = 0; i < tweetList.size(); i++)
                results = results.concat("\n\n" + i + ") " + tweetList.get(i).getFromUser() 
                        + ":  " + tweetList.get(i).getText());          
          return results;
      }
    
    private Twitter getTwitter() throws TwitterException, IOException{        
        return SearchTwitter.getTwitter();       
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jButton1 = new javax.swing.JButton();
        jButtonSearchTwitter = new javax.swing.JButton();
        jTextFieldGoogleSearchD = new javax.swing.JTextField();
        jTextFieldGoogleSearch = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaGoogleSearch = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaTwitter = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextAreaGoogleSearchC1 = new javax.swing.JTextArea();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextAreaGoogleSearchC2 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jComboBoxSimilaridade = new javax.swing.JComboBox();
        jComboBoxClusters = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jComboBoxClusters1Word1 = new javax.swing.JComboBox();
        jComboBoxClusters1Word2 = new javax.swing.JComboBox();
        jComboBoxClusters2Word2 = new javax.swing.JComboBox();
        jComboBoxClusters2Word1 = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jButtonSearchGoogleCluster1 = new javax.swing.JButton();
        jButtonSearchGoogleCluster2 = new javax.swing.JButton();
        jRadioButtonDBSCAN = new javax.swing.JRadioButton();
        jRadioButtonKMEANS = new javax.swing.JRadioButton();
        jRadioButtonOPTICS = new javax.swing.JRadioButton();
        jLabel11 = new javax.swing.JLabel();
        jTextFieldTamanhoCluster2 = new javax.swing.JTextField();
        jTextFieldTamanhoCluster1 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 13));
        jButton1.setText("Search Google");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButtonSearchTwitter.setFont(new java.awt.Font("Tahoma", 1, 13));
        jButtonSearchTwitter.setText("Search Twitter & Cluster");
        jButtonSearchTwitter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchTwitterActionPerformed(evt);
            }
        });

        jTextFieldGoogleSearchD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldGoogleSearchDActionPerformed(evt);
            }
        });

        jTextFieldGoogleSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldGoogleSearchActionPerformed(evt);
            }
        });

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTextAreaGoogleSearch.setColumns(20);
        jTextAreaGoogleSearch.setEditable(false);
        jTextAreaGoogleSearch.setRows(5);
        jScrollPane1.setViewportView(jTextAreaGoogleSearch);

        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTextAreaTwitter.setColumns(20);
        jTextAreaTwitter.setEditable(false);
        jTextAreaTwitter.setRows(5);
        jScrollPane2.setViewportView(jTextAreaTwitter);

        jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTextAreaGoogleSearchC1.setColumns(20);
        jTextAreaGoogleSearchC1.setEditable(false);
        jTextAreaGoogleSearchC1.setRows(5);
        jScrollPane3.setViewportView(jTextAreaGoogleSearchC1);

        jScrollPane4.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTextAreaGoogleSearchC2.setColumns(20);
        jTextAreaGoogleSearchC2.setEditable(false);
        jTextAreaGoogleSearchC2.setRows(5);
        jScrollPane4.setViewportView(jTextAreaGoogleSearchC2);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 13));
        jLabel1.setText("Google re-ranking Cluster 1 =>");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 13));
        jLabel3.setText("Google re-ranking Cluster 2 =>");

        jLabel5.setText("1st Word:");

        jLabel6.setText("Similaridade:");

        jComboBoxSimilaridade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cosseno", "Tanimoto" }));
        jComboBoxSimilaridade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxSimilaridadeActionPerformed(evt);
            }
        });

        jComboBoxClusters.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2", "3" }));
        jComboBoxClusters.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxClustersActionPerformed(evt);
            }
        });

        jLabel7.setText("# Clusters:");

        jLabel8.setText("2nd Word:");

        jComboBoxClusters1Word1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxClusters1Word1ActionPerformed(evt);
            }
        });

        jComboBoxClusters1Word2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "none" }));
        jComboBoxClusters1Word2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxClusters1Word2ActionPerformed(evt);
            }
        });

        jComboBoxClusters2Word2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "none" }));
        jComboBoxClusters2Word2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxClusters2Word2ActionPerformed(evt);
            }
        });

        jComboBoxClusters2Word1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxClusters2Word1ActionPerformed(evt);
            }
        });

        jLabel9.setText("1st Word:");

        jLabel10.setText("2nd Word:");

        jButtonSearchGoogleCluster1.setFont(new java.awt.Font("Tahoma", 1, 13));
        jButtonSearchGoogleCluster1.setText("Search Google Dinamically");
        jButtonSearchGoogleCluster1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchGoogleCluster1ActionPerformed(evt);
            }
        });

        jButtonSearchGoogleCluster2.setFont(new java.awt.Font("Tahoma", 1, 13));
        jButtonSearchGoogleCluster2.setText("Search Google Dinamically");
        jButtonSearchGoogleCluster2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchGoogleCluster2ActionPerformed(evt);
            }
        });

        jRadioButtonDBSCAN.setText("DBSCAN");
        jRadioButtonDBSCAN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonDBSCANActionPerformed(evt);
            }
        });

        jRadioButtonKMEANS.setSelected(true);
        jRadioButtonKMEANS.setText("kMeans");
        jRadioButtonKMEANS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonKMEANSActionPerformed(evt);
            }
        });

        jRadioButtonOPTICS.setText("OPTICS");
        jRadioButtonOPTICS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonOPTICSActionPerformed(evt);
            }
        });

        jLabel11.setText("Size of Cluster 2:");

        jTextFieldTamanhoCluster2.setBackground(new java.awt.Color(255, 255, 255));
        jTextFieldTamanhoCluster2.setEditable(false);
        jTextFieldTamanhoCluster2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldTamanhoCluster2ActionPerformed(evt);
            }
        });

        jTextFieldTamanhoCluster1.setEditable(false);
        jTextFieldTamanhoCluster1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldTamanhoCluster1ActionPerformed(evt);
            }
        });

        jLabel12.setText("Size of Cluster 1:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldGoogleSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1152, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButtonKMEANS)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jRadioButtonDBSCAN)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jRadioButtonOPTICS))
                            .addComponent(jButtonSearchTwitter, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                            .addComponent(jTextFieldGoogleSearchD, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBoxSimilaridade, 0, 139, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(18, 18, 18)
                                .addComponent(jComboBoxClusters, 0, 138, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 1149, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(jComboBoxClusters1Word1, 0, 145, Short.MAX_VALUE))
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                            .addComponent(jLabel8)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(75, 75, 75)
                                .addComponent(jComboBoxClusters1Word2, 0, 145, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldTamanhoCluster1, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
                            .addComponent(jButtonSearchGoogleCluster1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 1149, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(75, 75, 75)
                                .addComponent(jComboBoxClusters2Word2, 0, 144, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(18, 18, 18)
                                .addComponent(jComboBoxClusters2Word1, 0, 144, Short.MAX_VALUE))
                            .addComponent(jLabel3)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldTamanhoCluster2, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
                            .addComponent(jButtonSearchGoogleCluster2, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 1150, Short.MAX_VALUE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 1376, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextFieldGoogleSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextFieldGoogleSearchD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jRadioButtonKMEANS)
                            .addComponent(jRadioButtonDBSCAN)
                            .addComponent(jRadioButtonOPTICS))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBoxSimilaridade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jComboBoxClusters, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSearchTwitter)
                        .addGap(57, 57, 57))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jComboBoxClusters1Word1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jComboBoxClusters1Word2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(jTextFieldTamanhoCluster1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSearchGoogleCluster1)
                        .addGap(60, 60, 60))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(jComboBoxClusters2Word1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(jComboBoxClusters2Word2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(jTextFieldTamanhoCluster2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSearchGoogleCluster2))
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    String googleResults = googleSearchEngine.makeQuery(jTextFieldGoogleSearch.getText());
    jTextAreaGoogleSearch.setText(googleResults);
}//GEN-LAST:event_jButton1ActionPerformed

private void jButtonSearchTwitterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchTwitterActionPerformed
    QueryResult q = null;

    try {                      
        java.util.LinkedList<Tweet> tweetsMore = twitterSearchEngine.getListSearchResults(
            jTextFieldGoogleSearchD.getText(), lang);       
        jTextAreaTwitter.setText(this.getResultsString(tweetsMore));
        
        luceneIndex.addTweetsNoBlanks(tweetsMore);
        luceneIndex.analyzeTokens(tweetsMore);   
        
        luceneSearch = new TwitterLuceneSearch (directory);
        vectors = luceneSearch.makeVectors();
        //luceneSearch.printVectors();
        numVectors = luceneSearch.getNumVectors(); 
        
        if(this.jRadioButtonKMEANS.isSelected()){
            if ( jComboBoxClusters.getSelectedItem().equals("2") )
                clusteringAlgo = new Kmeans2(vectors, numVectors, luceneIndex.analyzeString(jTextFieldGoogleSearchD.getText()));
            else
                clusteringAlgo = new Kmeans3(vectors, numVectors, luceneIndex.analyzeString(jTextFieldGoogleSearchD.getText()));
            clusteringAlgo.run(this.getTipoDistancia(), TOP_QUANTOS);
            clearPopWordsBoxes();
            clearSearchDResults();
        
            setPopWordsBox(1, clusteringAlgo.getListPopWordsCluster(1));
            setPopWordsBox(2, clusteringAlgo.getListPopWordsCluster(2));
            this.jTextFieldTamanhoCluster1.setText(Integer.toString(clusteringAlgo.getSizeTopClusters()[0])) ;
            this.jTextFieldTamanhoCluster2.setText(Integer.toString(clusteringAlgo.getSizeTopClusters()[1])) ;
            
        }
        
        if(this.jRadioButtonDBSCAN.isSelected()){
            clusteringAlgo = new DBSCANv2(vectors,luceneIndex.analyzeString(jTextFieldGoogleSearchD.getText()));
            clusteringAlgo.run(this.getTipoDistancia(), TOP_QUANTOS);
            clearPopWordsBoxes();
            clearSearchDResults();
            setPopWordsBox(1, clusteringAlgo.getListPopWordsCluster(1));
            setPopWordsBox(2, clusteringAlgo.getListPopWordsCluster(2));
            this.jTextFieldTamanhoCluster1.setText(Integer.toString(clusteringAlgo.getSizeTopClusters()[0])) ;
            this.jTextFieldTamanhoCluster2.setText(Integer.toString(clusteringAlgo.getSizeTopClusters()[1])) ;
            
        }
        
        if(this.jRadioButtonOPTICS.isSelected()){
            clusteringAlgo = new OPTICS(vectors,luceneIndex.analyzeString(jTextFieldGoogleSearchD.getText()));
            clusteringAlgo.run(this.getTipoDistancia(), TOP_QUANTOS);
            clearPopWordsBoxes();
            clearSearchDResults();
            setPopWordsBox(1, clusteringAlgo.getListPopWordsCluster(1));
            setPopWordsBox(2, clusteringAlgo.getListPopWordsCluster(2));
            this.jTextFieldTamanhoCluster1.setText(Integer.toString(clusteringAlgo.getSizeTopClusters()[0])) ;
            this.jTextFieldTamanhoCluster2.setText(Integer.toString(clusteringAlgo.getSizeTopClusters()[1])) ;
        }

    } catch (IOException ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    } catch (TwitterException e1) {
        JOptionPane.showMessageDialog(null, "Error occurred during search, try again.");
    }
}//GEN-LAST:event_jButtonSearchTwitterActionPerformed

private void clearPopWordsBoxes(){
    jComboBoxClusters1Word1.removeAllItems();
    jComboBoxClusters1Word2.removeAllItems();
    jComboBoxClusters1Word2.addItem("none");
    jComboBoxClusters2Word1.removeAllItems();
    jComboBoxClusters2Word2.removeAllItems();
    jComboBoxClusters2Word2.addItem("none");
}

private void clearSearchDResults(){
    this.jTextAreaGoogleSearchC1.setText("");
    this.jTextAreaGoogleSearchC2.setText("");
}

private void setPopWordsBox (int cluster, Enumeration<String> words){
    String word;
    while (words.hasMoreElements()){
        word = words.nextElement();
        if (cluster == 1){            
            this.jComboBoxClusters1Word1.addItem(word);
            this.jComboBoxClusters1Word2.addItem(word);
        }
        else{
            this.jComboBoxClusters2Word1.addItem(word);
            this.jComboBoxClusters2Word2.addItem(word);
        }            
    }
}

private void jTextFieldGoogleSearchDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldGoogleSearchDActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextFieldGoogleSearchDActionPerformed

private void jTextFieldGoogleSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldGoogleSearchActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextFieldGoogleSearchActionPerformed

private void jComboBoxSimilaridadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxSimilaridadeActionPerformed

}//GEN-LAST:event_jComboBoxSimilaridadeActionPerformed

private void jComboBoxClustersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxClustersActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jComboBoxClustersActionPerformed

private void jComboBoxClusters1Word1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxClusters1Word1ActionPerformed

}//GEN-LAST:event_jComboBoxClusters1Word1ActionPerformed

private void jComboBoxClusters1Word2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxClusters1Word2ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jComboBoxClusters1Word2ActionPerformed

private void jComboBoxClusters2Word2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxClusters2Word2ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jComboBoxClusters2Word2ActionPerformed

private void jComboBoxClusters2Word1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxClusters2Word1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jComboBoxClusters2Word1ActionPerformed

private void jButtonSearchGoogleCluster1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchGoogleCluster1ActionPerformed
    String googleResults;
    if(jComboBoxClusters1Word2.getSelectedItem() == "none")
        googleResults = googleSearchEngine.makeQuery(jTextFieldGoogleSearchD.getText() + " " + 
                this.jComboBoxClusters1Word1.getSelectedItem());
    else
        googleResults = googleSearchEngine.makeQuery(jTextFieldGoogleSearchD.getText() + " " +
                this.jComboBoxClusters1Word1.getSelectedItem() + this.jComboBoxClusters1Word2.getSelectedItem());
    
    jTextAreaGoogleSearchC1.setText(googleResults);    
}//GEN-LAST:event_jButtonSearchGoogleCluster1ActionPerformed

private void jButtonSearchGoogleCluster2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchGoogleCluster2ActionPerformed
    String googleResults;
    if(jComboBoxClusters2Word2.getSelectedItem() == "none")
        googleResults = googleSearchEngine.makeQuery(jTextFieldGoogleSearchD.getText() + " " + 
                this.jComboBoxClusters2Word1.getSelectedItem());
    else
        googleResults = googleSearchEngine.makeQuery(jTextFieldGoogleSearchD.getText() + " " +
                this.jComboBoxClusters2Word1.getSelectedItem() + this.jComboBoxClusters2Word2.getSelectedItem());
    
    jTextAreaGoogleSearchC2.setText(googleResults);    
}//GEN-LAST:event_jButtonSearchGoogleCluster2ActionPerformed

private void jRadioButtonDBSCANActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonDBSCANActionPerformed
    if (jRadioButtonDBSCAN.isSelected()){
        jRadioButtonKMEANS.setSelected(false);
        jRadioButtonOPTICS.setSelected(false);
        this.jComboBoxClusters.setEnabled(false);
        if(this.jComboBoxSimilaridade.getItemCount() == 2)
            this.jComboBoxSimilaridade.addItem("Alternativa");
        
    }
}//GEN-LAST:event_jRadioButtonDBSCANActionPerformed

private void jRadioButtonKMEANSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonKMEANSActionPerformed
    if (jRadioButtonKMEANS.isSelected()){
        jRadioButtonDBSCAN.setSelected(false);
        jRadioButtonOPTICS.setSelected(false);
        if(this.jComboBoxSimilaridade.getItemCount() == 3)
            this.jComboBoxSimilaridade.removeItemAt(2);
        this.jComboBoxClusters.setEnabled(true);
    }
}//GEN-LAST:event_jRadioButtonKMEANSActionPerformed

private void jRadioButtonOPTICSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonOPTICSActionPerformed
    if (jRadioButtonOPTICS.isSelected()){
        jRadioButtonDBSCAN.setSelected(false);
        jRadioButtonKMEANS.setSelected(false);
        if(this.jComboBoxSimilaridade.getItemCount() == 2)
            this.jComboBoxSimilaridade.addItem("Alternativa");
        this.jComboBoxClusters.setEnabled(false);
    }
}//GEN-LAST:event_jRadioButtonOPTICSActionPerformed

private void jTextFieldTamanhoCluster2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldTamanhoCluster2ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextFieldTamanhoCluster2ActionPerformed

private void jTextFieldTamanhoCluster1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldTamanhoCluster1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextFieldTamanhoCluster1ActionPerformed

private int getTipoDistancia(){
    int tipoDistancia = 0;
    if(((String) jComboBoxSimilaridade.getSelectedItem()).equalsIgnoreCase("cosseno"))
        tipoDistancia = ClusteringAlgorithm.DISTANCIA_COSSENO;
    else if(((String) jComboBoxSimilaridade.getSelectedItem()).equalsIgnoreCase("alternativa"))
        tipoDistancia = ClusteringAlgorithm.DISTANCIA_ALTERNATIVA;
    else if(((String) jComboBoxSimilaridade.getSelectedItem()).equalsIgnoreCase("tanimoto"))
        tipoDistancia = ClusteringAlgorithm.DISTANCIA_TANIMOTO;
    return tipoDistancia;
}    

public static void main(String args[]) {
    
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(App.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {                    
                    new App().setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
                } catch (TwitterException ex) {
                    Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
                }                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonSearchGoogleCluster1;
    private javax.swing.JButton jButtonSearchGoogleCluster2;
    private javax.swing.JButton jButtonSearchTwitter;
    private javax.swing.JComboBox jComboBoxClusters;
    private javax.swing.JComboBox jComboBoxClusters1Word1;
    private javax.swing.JComboBox jComboBoxClusters1Word2;
    private javax.swing.JComboBox jComboBoxClusters2Word1;
    private javax.swing.JComboBox jComboBoxClusters2Word2;
    private javax.swing.JComboBox jComboBoxSimilaridade;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JRadioButton jRadioButtonDBSCAN;
    private javax.swing.JRadioButton jRadioButtonKMEANS;
    private javax.swing.JRadioButton jRadioButtonOPTICS;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextAreaGoogleSearch;
    private javax.swing.JTextArea jTextAreaGoogleSearchC1;
    private javax.swing.JTextArea jTextAreaGoogleSearchC2;
    private javax.swing.JTextArea jTextAreaTwitter;
    private javax.swing.JTextField jTextFieldGoogleSearch;
    private javax.swing.JTextField jTextFieldGoogleSearchD;
    private javax.swing.JTextField jTextFieldTamanhoCluster1;
    private javax.swing.JTextField jTextFieldTamanhoCluster2;
    // End of variables declaration//GEN-END:variables
}
